

Page({

  data: {
    detailList: {},
    genre: [],
    islikefill: false,
    isseen: false,
    countOfseen: 0
  },
  // 初始化
  onLoad: function (options) {
    this.getcurmovie(options)
    this.islike(options)
    this.isseen(options)
    this.getseencount(options)
    //this.countOfsee(options)
  },
  //统计看过的人数
  countOfsee: async function (options) {
    let db = wx.cloud.database()
    let count = await db.collection('collect').where({
      movieid: options.id,
      category: "seen"
    }).count()
    this.setData({
      countOfseen: count.total
    })
  },

  //获取当前电影的详情
  getcurmovie: function (options) {
    const db = wx.cloud.database();
    const tempmovie = db.collection("movies");
    let that = this;
    wx.showToast({
      title: '正在加载·····',
      icon: 'none',
      duration: 10000
    });
    //从集合中查询内容 
    tempmovie.where({
      "_id": options.id
    }).get({
      success: function (resp) {
        wx.hideToast()
        resp.data[0].genre = resp.data[0].genre.split("/");
        resp.data[0].actor = resp.data[0].actor.split("/");
        const reg = /[0-9\u4e00-\u9fa5]|\/|/g;
        resp.data[0].title = resp.data[0].title.match(reg).join('')
        that.setData({
          detailList: resp.data[0]
        })
      },
      fail: function (error) {
        return error
      }
    })
  },

  //收藏电影
  collect: async function (options) {
    let that = this;
    if (wx.getStorageSync('openid')) {
    } else {
    }
    const promise = new Promise((resolve, reject) => {
      const db = wx.cloud.database();
      const tempcollect1 = db.collection("collect");
      tempcollect1.where({
        "movieid": this.data.detailList._id,
        "useropenid": wx.getStorageSync('openid'),
        "category": "collect"
      }).get({
        success: res => {
          resolve(res)//会把进行中改变成已成功
        },
        fail: err => {
          reject(err)//会把进行中改变成已失败
        }
      })
    })
    promise.then(async res => {
      if (res.data.length) {
        wx.showToast({
          title: '已经收藏！',
        })
      }
      else {
        const db = wx.cloud.database();
        const tempcollect = db.collection("collect");
        await tempcollect.add({
          data: {
            "useropenid": wx.getStorageSync('openid'),
            "movieid": this.data.detailList._id,
            "category": "collect"
          },
          success: function (resp) {
            wx.showToast({
              title: '收藏成功！',
              icon: "success"
            })
            that.setData({
              islikefill: true
            })
          },
          fail: function (error) {
            wx.showToast({
              title: '请求失败！',
              icon: "none"
            })
          }
        })
      }
    }, err => {
      cosole.log(err)
    })
  },

  //点击看过，该电影的看过字段值加1，并且collect集合中增加该用户的看过信息
  seen: async function (options) {
    let that = this;
    if (wx.getStorageSync('openid')) {
    } else {
    }
    const promise = new Promise((resolve, reject) => {
      const db = wx.cloud.database();
      const tempcollect1 = db.collection("collect");
      tempcollect1.where({
        "movieid": this.data.detailList._id,
        "useropenid": wx.getStorageSync('openid'),
        "category": "seen"
      }).get({
        success: res => {
          resolve(res)//会把进行中改变成已成功
        },
        fail: err => {
          reject(err)//会把进行中改变成已失败
        }
      })
    })
    promise.then(async res => {
      if (res.data.length) {
        wx.showToast({
          title: '已经看过！',
        })
      }
      else {
        const db = wx.cloud.database();
        const tempcollect = db.collection("collect");
        await tempcollect.add({
          data: {
            "useropenid": wx.getStorageSync('openid'),
            "movieid": this.data.detailList._id,
            "category": "seen"
          },
          success: function (resp) {
            that.setData({
              isseen: true
            })

          },
          fail: function (error) {
            wx.showToast({
              title: '请求失败！',
              icon: "none"
            })
          }
        })
      }
    }, err => {
      cosole.log(err)
    })
  },

  //判断单签用户是否已经收藏该电影，如果收藏，界面的“收藏”变红，没有则反之
  islike: function (options) {
    let that = this;
    const promise = new Promise((resolve, reject) => {
      const db = wx.cloud.database();
      const tempcollect1 = db.collection("collect");
      tempcollect1.where({
        "movieid": options.id,
        "useropenid": wx.getStorageSync('openid'),
        "category": "collect"
      }).get({
        success: res => {
          resolve(res)
        },
        fail: err => {
          reject(err)
        }
      })
    })
    promise.then(async res => {
      if (res.data.length) {
        that.setData({
          islikefill: true
        })
      }
      else {
        that.setData({
          islikefill: false
        })
      }
    })
  },

  //判断单签用户是否已经看过该电影，如果看过，界面的“看过”变红，没有则反之
  isseen: function (options) {
    let that = this;
    const promise = new Promise((resolve, reject) => {
      const db = wx.cloud.database();
      const tempcollect1 = db.collection("collect");
      tempcollect1.where({
        "movieid": options.id,
        "useropenid": wx.getStorageSync('openid'),
        "category": "seen"
      }).get({
        success: res => {
          resolve(res)
        },
        fail: err => {
          reject(err)
        }
      })
    })
    promise.then(async res => {
      if (res.data.length) {
        that.setData({
          isseen: true
        })
      }
      else {
        that.setData({
          isseen: false
        })
      }
    })
  },

  //该电影的看过字段值加1
  getseencount: function (options) {
    let that = this;
    const db = wx.cloud.database();
    const tempcollect = db.collection("collect");
    tempcollect.where({
      "movieid": options.id,
      "category": "seen"
    }).get({
      success: function (res) {
        that.setData({
          countOfseen: res.data.length
        })
        this.countOfsee(options)
      },
      fail: function (error) {
        wx.showToast({
          title: '更新失败！' + error,
        })
      }

    })
  }

})